package com.castle.fortress.admin.rule.listRule.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.admin.utils.Sequence;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.rule.listRule.entity.RuleListRuleEntity;
import com.castle.fortress.admin.rule.listRule.dto.RuleListRuleDto;
import com.castle.fortress.admin.rule.listRule.dto.RuleListRuleWithInfoDto;
import com.castle.fortress.admin.rule.listRule.mapper.RuleListRuleMapper;
import com.castle.fortress.admin.rule.listRule.service.RuleListRuleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;

/**
 * 规则清单与规则关联表 服务实现类
 *
 * @author
 * @since 2025-04-22
 */
@Service
public class RuleListRuleServiceImpl extends ServiceImpl<RuleListRuleMapper, RuleListRuleEntity> implements RuleListRuleService {
    /**
    * 获取查询条件
    * @param ruleListRuleDto
    * @return
    */
    public QueryWrapper<RuleListRuleEntity> getWrapper(RuleListRuleDto ruleListRuleDto){
        QueryWrapper<RuleListRuleEntity> wrapper= new QueryWrapper();
        if(ruleListRuleDto != null){
            RuleListRuleEntity ruleListRuleEntity = ConvertUtil.transformObj(ruleListRuleDto,RuleListRuleEntity.class);
            wrapper.like(ruleListRuleEntity.getId() != null,"id",ruleListRuleEntity.getId());
            wrapper.like(ruleListRuleEntity.getListId() != null,"list_id",ruleListRuleEntity.getListId());
            wrapper.like(ruleListRuleEntity.getRuleId() != null,"rule_id",ruleListRuleEntity.getRuleId());
            wrapper.like(ruleListRuleEntity.getCreateTime() != null,"create_time",ruleListRuleEntity.getCreateTime());
            wrapper.like(ruleListRuleEntity.getUpdateTime() != null,"update_time",ruleListRuleEntity.getUpdateTime());
            wrapper.like(ruleListRuleEntity.getCreateUser() != null,"create_user",ruleListRuleEntity.getCreateUser());
            wrapper.like(ruleListRuleEntity.getUpdateUser() != null,"update_user",ruleListRuleEntity.getUpdateUser());
			wrapper.eq(ruleListRuleEntity.getIsDeleted() != null,"is_deleted",ruleListRuleEntity.getIsDeleted());
        }
        return wrapper;
    }


    @Override
    public IPage<RuleListRuleDto> pageRuleListRule(Page<RuleListRuleDto> page, RuleListRuleDto ruleListRuleDto) {
        QueryWrapper<RuleListRuleEntity> wrapper = getWrapper(ruleListRuleDto);
        Page<RuleListRuleEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<RuleListRuleEntity> ruleListRulePage=baseMapper.selectPage(pageEntity,wrapper);
        Page<RuleListRuleDto> pageDto = new Page(ruleListRulePage.getCurrent(), ruleListRulePage.getSize(),ruleListRulePage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(ruleListRulePage.getRecords(),RuleListRuleDto.class));
        return pageDto;
    }


    @Override
    public List<RuleListRuleDto> listRuleListRule(RuleListRuleDto ruleListRuleDto){
        QueryWrapper<RuleListRuleEntity> wrapper = getWrapper(ruleListRuleDto);
        List<RuleListRuleEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,RuleListRuleDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<RuleListRuleDto> ruleListRuleDtoList) {
        if (ruleListRuleDtoList == null || ruleListRuleDtoList.isEmpty()) {
            return false;
        }

        // 获取清单ID
        Long contractId = null;
        for (RuleListRuleDto dto : ruleListRuleDtoList) {
            if (dto.getContractId() != null) {
                contractId = dto.getContractId();
                break;
            }
        }

        // 如果存在清单ID，则先删除该清单下的所有规则关联
        if (contractId != null) {
            QueryWrapper<RuleListRuleEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("contract_id", contractId);
            this.remove(wrapper);
        }

        // 批量添加新的关联关系
        List<RuleListRuleEntity> entityList = new ArrayList<>();
        for (RuleListRuleDto dto : ruleListRuleDtoList) {
            RuleListRuleEntity entity = ConvertUtil.transformObj(dto, RuleListRuleEntity.class);
            entity.setId(Sequence.getInstance().nextId());
            entity.setCreateTime(new Date());
            entity.setIsDeleted(2);
            entityList.add(entity);
        }

        return super.saveBatch(entityList);
    }

    @Override
    public List<RuleListRuleWithInfoDto> listRuleListRuleWithInfo(RuleListRuleDto ruleListRuleDto) {
        if (ruleListRuleDto == null) {
            return new ArrayList<>();
        }
        return baseMapper.queryRuleListRuleWithInfo(ruleListRuleDto);
    }
}

